<!doctype html>
<html>
<!--
author:guojunyu
date:2019-05-22
desc:this demo is about blog. PLEASE NOTE:If you have trouble running it ,try any of the other demos or connect with auther.
A ny individuals and organizations and not for commercial use, professiona website for customized web site.
-->
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="shortcut icon" href="/static/images/bitbug_favicon.ico" type="image/x-icon" />
  <link href="/static/css/base.css" rel="stylesheet">
  <link href="/static/css/index.css" rel="stylesheet">
  <link href="/static/css/m.css" rel="stylesheet">
  <script src="/static/js/jquery.min.js" type="text/javascript"></script>
  <script src="/static/js/jquery.easyfader.min.js"></script>
  <script src="/static/js/scrollReveal.js"></script>
  <script src="/static/js/common.js"></script>
</head>

<body>
	<div id='xss' style="display:none"></div>
</body>

<script>

    token = window.localStorage.getItem('dnblog_token');
    username = window.localStorage.getItem('dnblog_user');
    var url = document.location.toString();
    var query_split = url.split('?');
    var query_set = '';
    if(query_split.length > 1){
        query_set = query_split[1];
    }
    var arrUrl = url.split("//");
    //当前访问的博客博主
    var blog_username = arrUrl[1].split('/')[1];

    if(query_set){
        var get_url = "http://127.0.0.1:8000/v1/topics/"+ blog_username + "?" + query_set;
    }else{
        var get_url = "http://127.0.0.1:8000/v1/topics/"+ blog_username;

    }

    var html_body = '';

    $.ajax({
        // 请求方式
        type:"get",
        // url
        url: get_url,
        beforeSend: function(request) {
            request.setRequestHeader("Authorization", token);
        },
        success:function (result) {
            if (200 == result.code){
                //alert("请求成功");
                //console.log(result);
                $(document).attr('title', '文章列表-'+ result.data.nickname+'的博客');

                header_html = makeHeader(blog_username, username)
                html_body += header_html

                html_body += '<div class="container">';
                html_body += '<div class="pagebg re"></div>';
                html_body += '<div class="container">';
                html_body += '<h1 class="t_nav"><span>不要轻易放弃。学习成长的路上，我们长路漫漫，只因学无止境。 </span>';

                html_body += '<p class="n1">发表博客</p>';
                html_body += '</h1>';
                html_body += '<div class="blogsbox">';
                html_body += '<br>';
                var topics = result.data.topics
                if(topics.length == 0){
                    html_body += '空空如也'

                } else {

                    for(var t in topics){

                        var title = topics[t].title;
			//var title_2 = $('#xss').html(title).text()
			//console.log(title_2)
                        var category = topics[t].category;
                        var created_time = topics[t].created_time;
                        var introduce = topics[t].introduce;
                        var author = topics[t].author;
                        html_body += '<div class="blogsbox">';
                        html_body += '<div class="blogs" data-scroll-reveal="enter bottom over 1s">';
                        ///<username>/topics/detail/<t_id>
                        var detail_url = '/' + blog_username + '/topics/detail/' + topics[t].id
                        html_body += '<h3 class="blogtitle"><a href="' + detail_url + '"' + ' target="_blank">' + title + '</a></h3>';
                        html_body += '<p class="blogtext">';
                        html_body += introduce;
                        html_body += '...</p>'
                        //html_body += '<p class="blogtext">'+ '12345' + '...</p>';
                        html_body += '<div class="bloginfo">';
                        html_body += '<ul>';
                        html_body += '<li class="author">' + author + '</li>';
                        html_body += '<li class="timer">' + created_time +  '</li>';
                        if(blog_username == username){
                            html_body += '<li class="" style="display:none"></li>';
                            html_body += '<li class="" style="padding-left:20px">编辑</li>';
                            html_body += '<li class="delete" style="padding-left:20px" data=' + topics[t].id +'>删除</li>';
                        }
                        html_body += '</ul>';
                        html_body += '</div>';
                        html_body += '</div>';
                        html_body += '</div>';
                    }

                }

                html_body += '</div>';
                $('body').html(html_body);
                //绑定登出
                loginOut()
                //初始化scroll
                scrollReveal.init();

                //下拉菜单
                $('.nav>li').hover(function () {
                    $(this).children('ul').stop(true, true).show(400);
                }, function () {
                    $(this).children('ul').stop(true, true).hide(400);
                });

                //添加删除事件
                $('.delete').on('click', function(){
                    var delete_id = $(this).attr('data');
                    var delete_url = "http://127.0.0.1:8000/v1/topics/"+ blog_username + "?" + 't_id=' + delete_id;
                    $.ajax({
                        // 请求方式
                        type:"delete",
                        url: delete_url,
                        beforeSend: function(request) {
                            request.setRequestHeader("Authorization", token);
                        },
                        success:function (result) {
                            if (200 == result.code){

                               alert('删除成功');
                               window.location.reload()

                            }else{

                               alert('删除失败');

                            }
                        }

                    })
                });

            }else{
                alert(result.error)
            }
          }
      });

</script>

</html>
